package jmine.tec.persist.jms;

import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.Message;
import javax.jms.MessageListener;

import bancosys.tec.persist.controller.TransactionalController;

/**
 * {@link MessageListener} que verifica se a mensagem sendo recebida eh uma mensagem de teste
 * 
 * @author takeshi
 */
public class TestMessageListener implements MessageListener, MessageDrivenBean {

    private MessageDrivenContext messageDrivenContext;

    private final MessageListener delegate;

    private TransactionalController transactionalController;

    /**
     * C'tor
     * 
     * @param delegate {@link MessageListener}
     */
    public TestMessageListener(MessageListener delegate) {
        this.delegate = delegate;
    }

    /**
     * {@inheritDoc}
     */
    public void onMessage(Message msg) {
        if (TestMessageSender.isTestMessage(msg)) {
            new TestMessageSender(this.getTransactionalController()).monitor(msg, String.valueOf(this.messageDrivenContext));
        } else {
            this.delegate.onMessage(msg);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void ejbRemove() {
    }

    /**
     * {@inheritDoc}
     */
    public void setMessageDrivenContext(MessageDrivenContext arg0) {
        this.messageDrivenContext = arg0;
    }

    /**
     * @param transactionalController the transactionalController to set
     */
    public void setTransactionalController(TransactionalController transactionalController) {
        this.transactionalController = transactionalController;
    }

    /**
     * @return the transactionalController
     */
    public TransactionalController getTransactionalController() {
        return transactionalController;
    }

}
